# -*- coding:utf-8 -*-
from application.model.User import  User
import os

from application import app,db
from flask import request,render_template,flash,abort,url_for,redirect,session,Flask,g

@app.route('/')
def show():
    userlist = User.query.all()
    return render_template('show.html',entries=userlist)

@app.route('/add',methods=['POST'])
def add_entry():
    if not session.get('logged_in'):
        abort(401)
    title = request.form['title']
    content = request.form['text']
    category = Category(title,content)
    db.session.add(category)
    db.session.commit()
    flash("添加分类成功")
    return redirect(url_for('show'))

@app.route('/login',methods=['GET','POST'])
def login():
    error = None
    if request.method == 'POST':
        nickname = request.form['nickname']
        password = request.form['password']
        user = User.query.filter_by(nickname=request.form['nickname']).first()
        passwd = User.query.filter_by(password=request.form['password']).first()

        if user is None:
            error = "用户名不存在"
        elif passwd is None:
            error = "密码错误"
        else:
            session['logged_in'] = True
            flash("登录成功")
            return redirect(url_for('show'))
    return render_template('login.html', error=error)


@app.route('/logout')
def logout():
    session.pop('logged_in', None)
    flash("推出成功")
    return redirect(url_for('show'))
